<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% projects = Project.visible.has_module(:dmsf).joins(:watchers).where(
  :watchers => { watchable_type: 'Project', user_id: @user&.id }) %>
<% folders = DmsfFolder.visible(:dmsf).joins(:watchers).where(
  :watchers => { watchable_type: 'DmsfFolder', user_id: @user&.id }) %>
<% files = DmsfFile.visible.joins(:watchers).where(
  :watchers => { watchable_type: 'DmsfFile', user_id: @user&.id }) %>
<% easy_extension = defined?(EasyExtensions) %>
<% unless easy_extension %>
  <h3><%= l(:label_dmsf_watched)%> (<%= projects.all.size + folders.all.size %>/<%=  files.all.size %>)</h3>
<% end %>
<% if projects.any? || folders.any? || files.any? %>
  <%= form_tag({}, data: { cm_url: dmsf_context_menu_path }) do %>
    <%= hidden_field_tag 'back_url', my_page_path, id: nil %>
    <table class="list context-menu-container" data-hascontextmenu="true">
      <thead>
        <tr>
          <% unless easy_extension %>
            <th class="checkbox hide-when-print">
              <%= check_box_tag 'check_all', '', false, class: 'toggle-selection',
                                title: "#{l(:button_check_all)}/#{l(:button_uncheck_all)}" %>
            </th>
          <% end %>
          <th><%= l(:field_project) %></th>
          <th><%= l(:label_document) %>/<%= l(:field_folder) %></th>
          <th><%= l(:field_folder) %></th>
          <% unless easy_extension %>
            <th></th>
          <% end %>
        </tr>
      </thead>
      <tbody>
        <% projects.each do |project| %>
          <tr id="project-<%= project.id %>" class="hascontextmenu">
            <% unless easy_extension %>
              <td class="checkbox hide-when-print">
                <%= check_box_tag 'ids[]', "project-#{project.id}", false, id: nil %>
              </td>
            <% end %>
            <td class="project">
              <%= link_to_project project %>
            </td>
            <td class="title">
              <%= link_to_project project, jump: 'dmsf', class: 'project' %>
            </td>
            <td class="title"></td>
            <td>
              <%= link_to_context_menu %>
            </td>
          </tr>
        <% end %>
        <% folders.each do |folder| %>
          <tr id="folder-<%= folder.id %>" class="hascontextmenu">
            <% unless easy_extension %>
              <td class="checkbox hide-when-print">
                <%= check_box_tag 'ids[]', "folder-#{folder.id}", false, id: nil %>
              </td>
            <% end %>
            <td class="project">
              <%= link_to_project folder.project %>
            </td>
            <td class="title">
              <%= link_to h(folder.title), dmsf_folder_path(id: folder.project, folder_id: folder),
                          class: 'icon icon-folder' %>
             </td>
             <td class="title">
               <% if folder.dmsf_folder %>
                 <%= link_to h(folder.dmsf_folder.title), dmsf_folder_path(id: folder.project, folder_id:
                   folder.dmsf_folder) %>
               <% else %>
                  <%= link_to l(:link_documents), dmsf_folder_path(id: folder.project) %>
               <% end %>
             </td>
            <td>
              <%= link_to_context_menu %>
            </td>
           </tr>
        <% end %>
        <% files.each do |file| %>
          <tr id="file-<%= file.id %>" class="hascontextmenu">
            <% unless easy_extension %>
              <td class="checkbox hide-when-print">
                <%= check_box_tag 'ids[]', "file-#{file.id}", false, id: nil %>
              </td>
            <% end %>
            <td class="project">
              <%= link_to_project(file.project) if file.project %>
            </td>
            <td class="title">
              <% icon_name = icon_for_mime_type(Redmine::MimeType.css_class_of(file.name)) %>
              <%= link_to sprite_icon(icon_name, h(file.title)), dmsf_file_path(id: file), class: 'icon icon-file' %>
            </td>
            <td class="title">
              <% if file.dmsf_folder %>
                <%= link_to h(file.dmsf_folder.title), dmsf_folder_path(id: file.project, folder_id: file.dmsf_folder) %>
              <% else %>
                <%= link_to l(:link_documents), dmsf_folder_path(id: file.project) %>
              <% end %>
            </td>
            <% unless easy_extension %>
              <td class="buttons">
                <%= link_to_context_menu %>
              </td>
            <% end %>
           </tr>
        <% end %>
      </tbody>
    </table>
  <% end %>
  <%= context_menu unless easy_extension %>
<% else %>
  <p class="nodata"><%= l(:label_no_data) %></p>
<% end %>
